﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using Microsoft.Phone.Tasks;
using System.Text;

namespace Nadia
{
    public partial class MyApp : PhoneApplicationPage
    {
        List<Allkpop> rss_items;       
        Allkpop item;
        Allkpop newsfeed;

        public MyApp()
        {
           InitializeComponent();
            WebClient Allkpopclient = new WebClient();
          //  Allkpopclient.Encoding = Encoding.GetEncoding("ISO-8859-1");
            Allkpopclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Allkpopclient_DownloadStringCompleted);
            Allkpopclient.DownloadStringAsync(new Uri("http://www.allkpop.com/feed", UriKind.RelativeOrAbsolute));
                   
        }

        void Allkpopclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
              
                  if (e.Error != null)
                  {
                      MessageBox.Show(e.Error.Message);//check ว่าได้รับรึป่าว คือดูว่าถ้ามีerror ก้อรีเทินออกไป
                      return;
                  }
                XElement AllkpopElement = XElement.Parse(e.Result);
                rss_items=new List<Allkpop>();

                  foreach( XElement value in AllkpopElement.Elements("channel").Elements("item")){
                      item = new Allkpop();
                      item.Title = value.Element("title").Value;
                      item.Description = Regex.Replace(value.Element("description").Value, @"&#\d\d\d\d;", String.Empty);
                      item.Link = value.Element("link").Value;
                      item.Date = Regex.Replace(value.Element("pubDate").Value,@"\+\d\d\d\d",String.Empty);
                      rss_items.Add(item);       
                 }
                 ListAllkpop.ItemsSource = rss_items.ToArray();
         }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
          {
              WebBrowserTask webBrowserTask = new WebBrowserTask();
              string input1 = input.Text;

              webBrowserTask.Uri = new Uri("http://www.allkpop.com/search?q="+input1);

              webBrowserTask.Show(); 

          }   

        private void Grid_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
          	NavigationService.Navigate(new Uri("/read_more.xaml?link=" + newsfeed.Link,
                UriKind.RelativeOrAbsolute));
        }

        private void ListAllkpop_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            newsfeed = (Allkpop)ListAllkpop.SelectedItem;
        }

    }
}
